package menu

import (
	"context"

	"mall/service/backend/api/internal/logic/utils"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/i18n"
	"mall/service/backend/rpc/backendclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetMenuByIdLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetMenuByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMenuByIdLogic {
	return &GetMenuByIdLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetMenuByIdLogic) GetMenuById(req *types.IDReq) (resp *types.SystemMenuInfoResp, err error) {
	data, err := l.svcCtx.BackendRpc.GetMenuById(l.ctx, &backendclient.IDReq{
		Id: req.Id,
	})
	if err != nil {
		return nil, err
	}

	option := utils.GetOptionByStatus(data.Status)

	return &types.SystemMenuInfoResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Data: &types.SystemMenuInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:         data.Id,
				CreatedAt:  data.CreatedAt,
				UpdatedAt:  data.UpdatedAt,
				DeletedAt:  data.DeletedAt,
				CanAccept:  option.CanAccept,
				CanReject:  option.CanReject,
				CanRemove:  option.CanRemove,
				CanRestore: option.CanRestore,
			},
			Trans:     l.svcCtx.Trans.Trans(l.ctx, *data.Meta.Title),
			ParentId:  data.ParentId,
			Type:      data.Type,
			Level:     data.Level,
			Path:      data.Path,
			Name:      data.Name,
			Redirect:  data.Redirect,
			Component: data.Component,
			Status:    data.Status,
			Sort:      data.Sort,
			Meta: types.SystemMenuMeta{
				Title:              data.Meta.Title,
				Icon:               data.Meta.Icon,
				HideMenu:           data.Meta.HideMenu,
				HideBreadcrumb:     data.Meta.HideBreadcrumb,
				IgnoreKeepAlive:    data.Meta.IgnoreKeepAlive,
				HideTab:            data.Meta.HideTab,
				FrameSrc:           data.Meta.FrameSrc,
				CarryParam:         data.Meta.CarryParam,
				HideChildrenInMenu: data.Meta.HideChildrenInMenu,
				Affix:              data.Meta.Affix,
				DynamicLevel:       data.Meta.DynamicLevel,
				RealPath:           data.Meta.RealPath,
			},
		},
	}, nil
}
